package com.jt.yygh.hospital.controller.api;

import com.jt.yygh.common.exception.BizServiceException;
import com.jt.yygh.common.helper.HttpRequestHelper;
import com.jt.yygh.common.result.Result;
import com.jt.yygh.common.result.ResultCodeEnum;
import com.jt.yygh.common.utils.MD5;
import com.jt.yygh.hospital.entity.hosp.Department;
import com.jt.yygh.hospital.entity.hosp.Hospital;
import com.jt.yygh.hospital.entity.hosp.Schedule;
import com.jt.yygh.hospital.service.DepartmentService;
import com.jt.yygh.hospital.service.HospitalService;
import com.jt.yygh.hospital.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jiang tao
 * @date 2022/2/28
 * @description:
 **/
@RestController
@RequestMapping("/api/hosp")
@Api("api")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("保存医院信息")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院系统传递过来的签名，签名进行md5加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalService.getSignkey(hoscode);
        // 把数据库查询签名进行MD5加密
        String signkeyMD5 = MD5.encrypt(signKey);
        if(!hospSign.equals(signkeyMD5)){
            System.out.println("signKey失败");
            return Result.fail(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中 + 变为了“” ，因此我们要转变回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData",logoData);
        //调用service的方法,
        String res = hospitalService.save(paramMap);
        return Result.success(res);

    }

    @ApiOperation("获取医院信息")
    @PostMapping("hospital/show")
    public Result<Hospital> hospital(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        validationParam(stringObjectMap);

        String hoscode = (String) stringObjectMap.get("hoscode");
        return Result.success(hospitalService.getByHoscode(hoscode));

    }

    @ApiOperation("上传科室")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        validationParam(stringObjectMap);
        departmentService.save(stringObjectMap);
        return Result.success(true);
    }

    @ApiOperation("获取科室分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        validationParam(paramMap);
        Map<String, Object> departmentQuery = new HashMap<>();
        departmentQuery.put("hoscode", paramMap.get("hoscode"));
        departmentQuery.put("depcode",paramMap.get("depcode"));
        int page = StringUtils.hasLength((String) paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.hasLength((String) paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
        departmentQuery.put("page", page);
        departmentQuery.put("limit", limit);


        Page<Department> departmentPage = departmentService.getDepartmentsByHoscodeAndDepcode(departmentQuery);
        return Result.success(departmentPage);

    }


    @ApiOperation("删除科室接口")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        validationParam(paramMap);

        //删除条件
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        departmentService.remove(hoscode, depcode);
        return Result.success(true);
    }


    @ApiOperation("上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        validationParam(paramMap);
        scheduleService.save(paramMap);
        return Result.success(true);
    }

    @ApiOperation("获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        validationParam(paramMap);

        Map<String, Object> scheduleQuery = new HashMap<>();
        scheduleQuery.put("limit", paramMap.get("limit"));
        scheduleQuery.put("page", paramMap.get("page"));
        scheduleQuery.put("hoscode", paramMap.get("hoscode"));
        scheduleQuery.put("depcode", paramMap.get("depcode"));

        Page<Schedule> schedulePage = scheduleService.getSchedulePage(scheduleQuery);
        return Result.success(schedulePage);
    }

    @ApiOperation("删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        validationParam(paramMap);
        scheduleService.remove((String) paramMap.get("hoscode"), (String) paramMap.get("hosScheduleId"));
        return Result.success(true);

    }








    /**
     * 参数校验，相当于身份认证
     * @param stringObjectMap
     */
    private void validationParam(Map<String, Object> stringObjectMap){
        //参数校验
        String hoscode = (String) stringObjectMap.get("hoscode");
        if(StringUtils.hasLength(hoscode)){
            throw new BizServiceException(ResultCodeEnum.PARAM_ERROR);
        }
        if(!HttpRequestHelper.isSignEquals(stringObjectMap, hospitalService.getSignkey(hoscode))){
            throw new BizServiceException(ResultCodeEnum.SIGN_ERROR);
        }
    }





}
